package models

import (
	"encoding/json"
	"github.com/astaxie/beego/orm"
	"strconv"
	"wego/utils"
)

type AuthGroupAccess struct {
	Id      int64  `json:"id"`
	Uid     int64  `json:"uid"`
	GroupId string `json:"group_id"`
}

type GroupAccessDelParams struct {
	Uid int64 `json:"uid"`
	Gid int64 `json:"gid"`
}

func GetGroupId(uid int64) string {
	o := orm.NewOrm()
	group := AuthGroupAccess{Uid: uid}
	err := o.Read(&group, "Uid")
	if err != nil {
		return ""
	}
	return group.GroupId
}

func MemberDel(params *GroupAccessDelParams) error {
	o := orm.NewOrm()
	data := AuthGroupAccess{
		Uid: params.Uid,
	}
	err := o.Read(&data, "Uid")
	if err == nil {
		var arr []int64
		_ = json.Unmarshal([]byte(data.GroupId), &arr)
		arr = utils.RemoveElement(arr, params.Gid)
		str := "["
		for i := 0; i < len(arr); i++ {
			str += strconv.Itoa(int(arr[i]))
		}
		str += "]"
		data.GroupId = str
		_, err = o.Update(&data)
	}
	return err
}
